/*
 * Copyright (c) 2022, yifengling0. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. No personal names or organizations' names associated with the
 *    Atomthreads project may be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE ATOMTHREADS PROJECT AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */
#include <Arduino.h>
#include "atom.h"
#include "console.h"
#define ATOM_NONE_STANDARDIO
#include "drv_common.h" 
#include "debug.h"

#ifdef COM_USE_CONSOLE

static enConsoleResult reboot(int argc, char **argv)
{
    esp_restart(); 
    return CE_OK;
}

static enConsoleResult cpuinfo(int argc, char **argv)
{
    uint64_t chipid;

    chipid = DrvCommonGetCpuId();

    KPrintf("Chip ID = %04X",(uint16_t)(chipid>>32));//print High 2 bytes
    KPrintf("%08X\r\n",(uint32_t)chipid);//print Low 4bytes. 

    KPrintf("total heap size = %u\r\n",ESP.getHeapSize());
    KPrintf("available heap = %u\r\n",ESP.getFreeHeap());
    KPrintf("lowest level of free heap since boot = %u\r\n",ESP.getMinFreeHeap());
    KPrintf("largest block of heap that can be allocated at once = %u\r\n",ESP.getMaxAllocHeap());

    KPrintf("total Psram size = %u\r\n",ESP.getPsramSize());
    KPrintf("available Psram = %u\r\n",ESP.getFreePsram());
    KPrintf("lowest level of free Psram since boot = %u\r\n",ESP.getMinFreePsram());
    KPrintf("largest block of Psram that can be allocated at once = %u\r\n",ESP.getMinFreePsram());

    KPrintf("Chip Revision = %u\r\n",ESP.getChipRevision());
    KPrintf("CpuFreqMHz = %u\r\n",ESP.getCpuFreqMHz());   
    KPrintf("Cycle Count = %u\r\n",ESP.getCycleCount());
    KPrintf("SdkVersion = %s\r\n",ESP.getSdkVersion());    

    return CE_OK;
}

#endif

uint64_t DrvCommonGetCpuId(void)
{
    uint64_t chipid = ESP.getEfuseMac();

    return (chipid);
}


BGEIN_DEF_CMD(_commCommands)
    CMD_DEF(reboot, "reboot the system")
    CMD_DEF(cpuinfo, "print cpu infomation")
END_DEF_CMD()

void DrvCommonInit(void)
{
    CONSOLE_REG_CMD(_commCommands);
}
